package com.czk.rdd.operator.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/10 14:28
 */
object WordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sc = new SparkContext(sparkConf)
    wordcount1(sc)
  }

  def wordcount1(sc: SparkContext): Unit = {
    val rdd: RDD[String] = sc.makeRDD(List("HELLO,JAVA", "HELLO,SPARK"))
    // 扁平化处理
    val words = rdd.flatMap(_.split((" ")))

    val unit: RDD[(String, Iterable[String])] = words.groupBy(word => word)

    val wordCount: RDD[(String, Int)] = unit.mapValues(iter => iter.size)

    println(wordCount.values)
  }
}
